const {comMysql} = require("../db/mysql")

const xss = require("xss")

const addAdvice=(req,res)=>{
  let {content,username} = req.body;
  content = xss(content)
  let time = new Date().getTime()
  let sql =  `insert into advice(username,content,time) values (?,?,?)`
  
  
  let arr = [username,content,time]

  return comMysql(sql,arr)
}

const delAdvice=(req,res)=>{
  let {id,username} = req.body
  if(username=="2628735401"){
let sql = `delete from advice where id = ?`
  let arr = [id]
  return comMysql(sql,arr)
  }else{
    let sql = `delete from advice where id = ? and username = ?`
  let arr = [id ,username]
  return comMysql(sql,arr)
  }
  
}


const searchAdvice=(req,res)=>{
  let sql = `SELECT users.realname,advice.username,advice.content,advice.time,advice.id from advice LEFT JOIN users on advice.username = users.username ORDER BY time DESC`
  return comMysql(sql)

}
const choosesearch=(req,res)=>{
    let {username} = req.query
    let sql = `select * from advice where username like "%${username}%"`

    return comMysql(sql)
}



module.exports={
  addAdvice,
  delAdvice,
  searchAdvice,
  choosesearch
}